<template>
	<view class="detail-container" :class="{ themeStyle, 'safe-area': isIphoneX }">
		<!-- 订单状态 -->
		<view class="status-wrap ns-bg-color">
			<view>
				<text class="status-name">{{ orderData.order_status_name }}</text>
			</view>
			<block v-if="orderData.order_status == 0">
				<view>
					<text class="desc">
						需付款：
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text class="ns-font-size-lg price">{{ orderData.pay_money }}</text>
					</text>
				</view>
				<view class="operation-group"><view class="operation-btn ns-text-color" @click="operation('orderPay')">去支付</view></view>
			</block>
			<block v-if="orderData.order_status == 3">
			<view class="clockrun">
				还剩<uni-count-down
					:day="time.d"
					:hour="time.h"
					:minute="time.m"
					:second="time.s"
					color="#fff"
					:showColon="!1"
					borderColor="rgba(0, 0, 0, 0)"
					splitorColorClass="ns-text-color"
					splitorColor="#fff !important"
					backgroundColorClass="ns-bg-color"
				/>自动收货
			</view>
			</block>
		</view>

		<!-- 地址信息 -->
		<view class="address-wrap active">
			<view class="icon"><view class="iconfont icondizhi"></view></view>
			<view class="address-info">
				<view class="info">
					<text>收货人：{{ orderData.name }}</text>
					<text>{{ orderData.mobile }}</text>
				</view>
				<view class="detail">
					<text>收货地址：{{ orderData.full_address }} {{ orderData.address }}</text>
				</view>
			</view>
		</view>

		<!-- 店铺 -->
		<view class="site-wrap">
			<view class="site-header" @click="toShopDetail(orderData.site_id)">
				<view class="iconfont icondianpu"></view>
				<text class="site-name">{{ orderData.site_name }}</text>
				<view class="iconfont iconright"></view>
			</view>
			<view class="site-body">
				<view class="goods-wrap" v-for="(goodsItem, goodsIndex) in orderData.order_goods" :key="goodsIndex">
					<navigator hover-class="none" class="goods-img" :url="'/pages/goods/detail/detail?sku_id=' + goodsItem.sku_id">
						<image :src="$util.img(goodsItem.sku_image, { size: 'mid' })" @error="imageError(goodsIndex)" mode="aspectFill"></image>
					</navigator>
					<view class="goods-info">
						<navigator hover-class="none" :url="'/pages/goods/detail/detail?sku_id=' + goodsItem.sku_id" class="goods-name">{{ goodsItem.sku_name }}</navigator>
						<view class="goods-sub-section">
							<view>
								<text class="goods-price ns-text-color">
									<text v-if="goodsItem.point != 0">{{ goodsItem.point }}积分</text>
								</text>
								<text class="goods-price ns-text-color">
									<text v-if="goodsItem.price != 0">{{ goodsItem.price }}余额</text>
								</text>
								<text class="goods-price ns-text-color">
									<text v-if="goodsItem.red_point != 0">{{ goodsItem.red_point }}红积分</text>
								</text>
								<text class="goods-price ns-text-color">
									<text v-if="goodsItem.junzheng_ticket != 0">{{ goodsItem.junzheng_ticket }}君政券</text>
								</text>
							</view>
							<view>
								<text>
									<text class="iconfont iconclose"></text>
									{{ goodsItem.num }}
								</text>
							</view>
							
						</view>
						<text class="tip" v-if="goodsItem.is_present">赠品</text>
						<view class="goods-operation">
							<block v-if="orderData.is_enable_refund">
								<!-- <navigator  hover-class="none"
									:url="'/otherpages/order/refund/refund?order_goods_id=' + goodsItem.order_goods_id"
									v-if="goodsItem.refund_status == 0 || goodsItem.refund_status == -1"
								> -->
								<navigator
									hover-class="none"
									:url="'/otherpages/order/refund/refund?order_goods_id=' + goodsItem.order_goods_id"
									v-if="goodsItem.refund_status == 0"
								>
									<view class="order-box-btn">退款</view>
								</navigator>
								<navigator hover-class="none" :url="'/otherpages/order/refund_detail/refund_detail?order_goods_id=' + goodsItem.order_goods_id" v-else>
									<view class="order-box-btn">查看退款</view>
								</navigator>
							</block>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 联系客服 -->
		<view class="kefu">	
			<block v-if="!addonIsExit.servicer">
				<!-- #ifdef MP-WEIXIN -->
				<button type="default" hover-class="none" open-type="contact"></button>
				<!-- #endif -->
				<view >
					<text class="iconfont iconziyuan"></text>
					<text>联系客服</text>
				</view>
			</block>
			<block v-else>
				<view @click="goConnect">
					<text class="iconfont iconziyuan"></text>
					<text>联系客服</text>
				</view>
			</block>
		</view>

		<!-- 订单概况 -->
		<view class="order-summary">
			<view class="order-cell">
				<text class="tit">订单编号：</text>
				<view class="box">
					<text class="ns-text-color-black">{{ orderData.order_no }}</text>
					<view class="copy" @click="$util.copy(orderData.order_no)">复制</view>
				</view>
			</view>
			<view class="order-cell">
				<text class="tit">订单交易号：</text>
				<view class="box">
					<text class="ns-text-color-black">{{ orderData.out_trade_no }}</text>
				</view>
			</view>
			<view class="order-cell">
				<text class="tit">创建时间：</text>
				<view class="box">
					<text class="ns-text-color-black">{{ $util.timeStampTurnTime(orderData.create_time) }}</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.close_time > 0">
				<text class="tit">关闭时间：</text>
				<view class="box">
					<text class="ns-text-color-black">{{ $util.timeStampTurnTime(orderData.close_time) }}</text>
				</view>
			</view>
			<block v-if="orderData.pay_status > 0">
				<view class="hr"></view>
				<view class="order-cell">
					<text class="tit">支付方式：</text>
					<view class="box">
						<text class="ns-text-color-black">{{ orderData.pay_type_name }}</text>
					</view>
				</view>
				<view class="order-cell">
					<text class="tit">支付时间：</text>
					<view class="box">
						<text class="ns-text-color-black">{{ $util.timeStampTurnTime(orderData.pay_time) }}</text>
					</view>
				</view>
			</block>
			<view class="hr"></view>
			<view class="order-cell">
				<text class="tit">配送方式：</text>
				<view class="box">
					<text class="ns-text-color-black">{{ orderData.delivery_type_name }}</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.buyer_message != ''">
				<text class="tit">买家留言：</text>
				<view class="box">
					<text class="ns-text-color-black">{{ orderData.buyer_message }}</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.promotion_type_name != ''">
				<text class="tit">店铺活动：</text>
				<view class="box">
					<text class="ns-text-color-black">{{ orderData.promotion_type_name }}</text>
				</view>
			</view>
		</view>

		<!-- 订单金额 -->
		<view class="order-money">
			<view class="order-cell" v-if="orderData.goods_money != 0">
				<text class="tit">商品总余额</text>
				<view class="box align-right">
					<text class="ns-text-color-black">
						{{ orderData.goods_money }}
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.point != 0">
				<text class="tit">商品总积分</text>
				<view class="box align-right" >
					<text class="ns-text-color-black">
						{{ orderData.point }}
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.red_point != 0">
				<text class="tit">商品总红积分</text>
				<view class="box align-right" >
					<text class="ns-text-color-black">
						{{ orderData.red_point  }}
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.junzheng_ticket != 0">
				<text class="tit">商品总君政券</text>
				<view class="box align-right" >
					<text class="ns-text-color-black">
						{{ orderData.junzheng_ticket }}
					</text>
				</view>
			</view>
			
			<view class="order-cell">
				<text class="tit">运费</text>
				<view class="box align-right">
					<text class="ns-text-color">
						<text class="operator">+</text>
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text>{{ orderData.delivery_money }}</text>
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.invoice_money > 0">
				<text class="tit">税费</text>
				<view class="box align-right">
					<text class="ns-text-color">
						<text class="operator">+</text>
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text>{{ orderData.invoice_money }}</text>
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.invoice_delivery_money > 0">
				<text class="tit">发票运费</text>
				<view class="box align-right">
					<text class="ns-text-color">
						<text class="operator">+</text>
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text>{{ orderData.invoice_delivery_money }}</text>
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.adjust_money != 0">
				<text class="tit">订单调整</text>
				<view class="box align-right">
					<text class="ns-text-color">
						<text class="operator" v-if="orderData.adjust_money < 0">-</text>
						<text class="operator" v-else>+</text>
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text>{{ orderData.adjust_money | abs }}</text>
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.promotion_money > 0">
				<text class="tit">优惠</text>
				<view class="box align-right">
					<text class="ns-text-color">
						<text class="operator">-</text>
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text>{{ orderData.promotion_money }}</text>
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.coupon_money > 0">
				<text class="tit">店铺优惠券</text>
				<view class="box align-right">
					<text class="ns-text-color">
						<text class="operator">-</text>
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text>{{ orderData.coupon_money }}</text>
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.platform_coupon_total_money > 0">
				<text class="tit">平台优惠券</text>
				<view class="box align-right">
					<text class="ns-text-color">
						<text class="operator">-</text>
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text>{{ orderData.platform_coupon_total_money }}</text>
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.balance_money > 0">
				<text class="tit">使用余额</text>
				<view class="box align-right">
					<text class="ns-text-color">
						<text class="operator">-</text>
						<text class="ns-font-size-sm">{{ $lang('common.currencySymbol') }}</text>
						<text>{{ orderData.balance_money }}</text>
					</text>
				</view>
			</view>
			<view class="order-cell" v-if="orderData.balance != 0">
				<text class="tit">订单总余额</text>
				<view class="box align-right">
					<text class="ns-text-color-black">
						{{ orderData.balance }}
					</text>
				</view>
			</view>
		</view>

		<view class="height-box"></view>

		<view class="order-operation bottom-safe-area" v-if="orderData.action.length > 0">
			<view class="order-box-btn white" v-if="orderData.is_evaluate == 1" @click="operation('memberOrderEvaluation')">
				<text v-if="orderData.evaluate_status == 0">评价</text>
				<text v-else-if="orderData.evaluate_status == 1">追评</text>
			</view>
			<view class="order-box-btn white" v-for="(operationItem, operationIndex) in orderData.action" :key="operationIndex" @click="operation(operationItem.action)">
				{{ operationItem.title }}
			</view>
		</view>
		<view class="order-operation bottom-safe-area" v-else-if="orderData.action.length == 0 && orderData.is_evaluate == 1">
			<view class="order-box-btn white" @click="operation('memberOrderEvaluation')">
				<text v-if="orderData.evaluate_status == 0">评价</text>
				<text v-else-if="orderData.evaluate_status == 1">追评</text>
			</view>
		</view>

		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
import orderMethod from '../public/js/orderMethod.js';
import globalConfig from 'common/js/golbalConfig.js'
import uniCountDown from '@/components/uni-count-down/uni-count-down.vue';
export default {
	components: {},
	data() {
		return {
			isIphoneX: false,
			orderId: 0,
			orderData: {
				action: []
			},
			time:{
				day:0,
				hour:0,
				minute:0,
				second:0
			}
		};
	},
	components:[uniCountDown],
	onLoad(option) {
		if (option.order_id) this.orderId = option.order_id;
	},
	onShow() {
		// 刷新多语言
		this.$langConfig.refresh();

		this.isIphoneX = this.$util.uniappIsIPhoneX();

		if (uni.getStorageSync('token')) {
			this.getOrderData();
		} else {
			this.$util.redirectTo('/pages/login/login/login', {
				back: '/pages/order/detail/detail?order_id=' + this.orderId
			});
		}
	},
	mixins:[globalConfig,orderMethod],
	methods: {
		//联系客服
		goConnect(){
			let that=this;
			if (uni.getStorageSync('token')) {
				let data = {
					siteId: that.orderData.site_id,
					orderId: that.orderData.order_id
				};
				that.$util.redirectTo('/otherpages/chat/room/room', data);
			}else{
				that.$util.redirectTo('/pages/login/login/login')
			}
		},
		getOrderData() {
			this.$api.sendRequest({
				url: '/api/order/detail',
				data: {
					order_id: this.orderId
				},
				success: res => {
					uni.stopPullDownRefresh();
					if(res.code==-1&&res.error_code=='TOKEN_ERROR'){
						this.$util.showToast({'title':'登录失败'});
						let pageRouter=getCurrentPages();
						let data=pageRouter[pageRouter.length-1].options;
						let back=pageRouter[pageRouter.length-1].route;
						data.back=back;
						setTimeout(()=>{
							this.$util.redirectTo('/pages/login/login/login', data);
						},1500)
						return;
					}
					if (res.code >= 0) {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
						this.orderData = res.data;
						
						var currtime = new Date().getTime() / 1000;
						var endtime = this.orderData.take_delivery_execute_time;
						var timeSub = endtime - currtime;
						console.log(currtime+'-'+endtime,"==="+timeSub);
						this.time = this.$util.countDown(timeSub);
						console.log(timeSub,456)
					} else {
						this.$util.showToast({
							title: '未获取到订单信息!！',
							success: () => {
								setTimeout(() => {
									this.$util.redirectTo('/pages/order/list/list');
								}, 1500);
							}
						});
					}
				},
				fail: res => {
					uni.stopPullDownRefresh();
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		/**
		 * 下拉刷新
		 */
		onPullDownRefresh() {
			this.getOrderData();
		},
		operation(action) {
			switch (action) {
				case 'orderPay': // 支付
					this.orderPay(this.orderData);
					break;
				case 'orderClose': //关闭
					this.orderClose(this.orderData.order_id, () => {
						this.getOrderData();
					});
					break;
				case 'memberTakeDelivery': //收货
					this.orderDelivery(this.orderData.order_id, () => {
						this.getOrderData();
					});
					break;
				case 'trace': //查看物流
					this.$util.redirectTo('/pages/order/logistics/logistics', {
						order_id: this.orderData.order_id
					});
					break;
				case 'memberOrderEvaluation': //评价
					this.$util.redirectTo('/otherpages/order/evaluate/evaluate', {
						order_id: this.orderData.order_id
					});
					break;
				case 'extendTakeDelivery': //延长收回
					this.orderExtendTakeDelivery(this.orderData.order_id, () => {
						this.getOrderData();
					});
					break;
			}
		},
		imageError(index) {
			this.orderData.order_goods[index].sku_image = this.$util.getDefaultImage().default_goods_img;
			this.$forceUpdate();
		},
		toShopDetail(e) {
			this.$util.redirectTo('/otherpages/shop/index/index', { site_id: e });
		}
	},
	filters: {
		abs(value) {
			return Math.abs(parseFloat(value)).toFixed(2);
		},
		// 转化时间字符串
		timeStr(val) {
			var h = parseInt(val / 3600).toString();
			var m = parseInt((val % 3600) / 60).toString();
			if (m.length == 1) {
				m = '0' + m;
			}
			if (h.length == 1) {
				h = '0' + h;
			}
			return h + ':' + m;
		}
	}
};
</script>

<style lang="scss">
@import '../public/css/detail.scss';
</style>
